<?php

namespace app\admin\controller\house;

use app\api\model\house\HouseManage;
use app\api\model\house\HouseManageCategory;
use app\api\model\house\HouseMediaCategory;
use app\api\model\house\HouseParkComplement;
use app\api\model\house\HouseParkMedia;
use app\common\controller\Backend;

/**
 * 园区
 *
 * @icon fa fa-circle-o
 */
class Housepark extends Backend
{

    /**
     * Housepark模型对象
     * @var \app\admin\model\house\HousePark
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\house\HousePark;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /*public function index()
    {
        $house_id = $this->request->param('house_id');
        $this->assignconfig('house_id', $house_id);
        return parent::index();
    }*/

    //园区详情
    public function detail($ids = null)
    {
        $row = $this->model::get($ids);

        if (!$row)$this->error('园区不存在');
        $park_id = $row->park_id;
        $this->view->assign("row", $row);

        //图片
        $media_list = [];
        $media_category = HouseMediaCategory::where(['type'=>1])->order('weigh desc')->field('id,name')->select();

        foreach ($media_category as $k=>$v){
            $list = HouseParkMedia::where(['park_id'=>$park_id,'category_id'=>$v['id']])->select();

            if (count($list)){
                $media_list[] = [
                    'title'=>$v['name'],
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("media_list", $media_list);

        //园区配套
        $house_park_complement_model = new HouseParkComplement();
        $type_list = $house_park_complement_model->getTypeList();
        $park_complement_list = [];
        foreach ($type_list as $k=>$v){
            $list = HouseParkComplement::where(['type'=>$k])
                ->where('id','in',$row->complement_id)
                ->order('weigh desc')->field('name')->select();
            if (count($list)>0){
                $park_complement_list[] = [
                    'title'=>$v,
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("park_complement_list", $park_complement_list);
        //物业管理
        $manage_category_list = HouseManageCategory::order('weigh desc')->field('id,name')->select();
        foreach ($manage_category_list as $k=>$v){
            $manage_category_list[$k]['child'] = HouseManage::where('id','in',$row->manage_id)
                ->order('weigh desc')->field('name')->select();
        }
        $this->view->assign("manage_list", $manage_category_list);
        return $this->view->fetch();
    }

    public function recommend($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_recommend=1;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function cancelrecommend($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_recommend=0;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function settop($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_top=1;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function canceltop($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_top=0;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function change($ids = null)
    {
        $row = $this->model->get($ids);

        $row->can_change=0;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}
